import { createRouter, createWebHistory } from 'vue-router'
import {authService} from '@/service/AuthService'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    { path: "/", redirect: "/main"},
    { path: '/main', name: "main", component: () => import('@/views/Main.vue')},
    { path: '/login', name: "login", component: () => import('@/views/Login.vue') },
    { path: '/registration', name: "registration", component: () => import('@/views/Registration.vue')},
    { path: '/orders', name: "orders", component: () => import('@/views/Orders.vue'),meta:{ requiresAuth: true }},
    { path: '/myorders', name: "myorders", component: () => import('@/views/Myorders.vue'),meta:{ requiresAuth: true }},
    { path: '/pay', name: "pay", component: () => import('@/views/Pay.vue'),meta:{ requiresAuth: true }},
    { path: '/businesslistings', name: "businesslistings", component: () => import('@/views/BusinessListings.vue'),meta:{ requiresAuth: true }},
    { path:'/bussinessinformation', name: "businessinformation", component: () => import('@/views/BusinessInformation.vue'),meta:{ requiresAuth: true }},
  ],
})

router.beforeEach((to, from) => {
  if (to.meta.requiresAuth && !authService.isLoggedIn) {
    return {name: "login"};
  }
})

export default router
